package com.lbn.jfqsc.app.message.action.discuss;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lbn.baseframe.base.BaseActivity;
import com.lbn.baseframe.base.BaseRecyclerAdapter;
import com.lbn.baseframe.ui.NAppbar;
import com.lbn.baseframe.utils.EasyToast;
import com.lbn.jfqsc.App;
import com.lbn.jfqsc.R;
import com.lbn.jfqsc.app.login.LoginActivity;
import com.lbn.jfqsc.app.message.adapter.DiscussAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DiscussActivity extends BaseActivity<DiscussPresenter> implements IDiscussView,
        XRecyclerView.LoadingListener, BaseRecyclerAdapter.OnItemClickListener {

    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;
    @BindView(R.id.comment_content_et)
    EditText commentContentEt;
    @BindView(R.id.send)
    Button send;
    private DiscussAdapter adapter;

    private String actId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discuss);
        ButterKnife.bind(this);
        setAppbar(new NAppbar(this));
        appbar.openBack();
        appbar.setTitle("社团活动");
    }


    @Override
    public DiscussPresenter createPresenter() {
        return new DiscussPresenter();
    }

    public static void startActivity(Context context, String actId) {
        Intent intent = new Intent(context, DiscussActivity.class);
        intent.putExtra("ActId", actId);
        context.startActivity(intent);
    }


    @OnClick(R.id.send)
    public void onClick(View v) {
        if (!App.isMerchant()) {
            LoginActivity.startActivity(this);
            return;
        }
        mPresenter.comment(getIntent().getStringExtra("ActId"), commentContentEt.getText().toString().trim());
    }

    @Override
    public void commentSuccess() {
        commentContentEt.setText("");
        page = 1;
        mPresenter.commentList(actId, page);
    }

    @Override
    public void InvalidData() {
        EasyToast.showShort(this, "请输入评论内容");
    }

    @Override
    public void onPresenterCreated() {
        super.onPresenterCreated();

        actId = getIntent().getStringExtra("ActId");
        adapter = new DiscussAdapter(this, null);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingListener(this);
        recyclerView.setRefreshing(true);
    }


    @Override
    public void onItemClick(View itemView, int pos) {
    }

    int page = 1;

    @Override
    public void onRefresh() {
        page = 1;
        mPresenter.commentList(actId, page);
    }

    @Override
    public void onLoadMore() {
        page++;
        mPresenter.commentList(actId, page);
    }

    @Override
    public void isMore(boolean b) {
        recyclerView.setNoMore(!b);
    }

    @Override
    public void setData(List list) {
        adapter.setData(list);
        recyclerView.refreshComplete();
    }

    @Override
    public void addData(List list) {
        adapter.addAll(list);
        recyclerView.loadMoreComplete();
    }
}
